package com.txtdhis.producer.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txtdhis.common.util.PageUtils;
import com.txtdhis.producer.mapper.UserMapper;
import com.txtdhis.producer.service.UserService;
import com.txtdhis.common.domain.User;
import com.txtdhis.producer.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.txtdhis.common.domain.page.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.txtdhis.common.domain.page.PageResult;
import java.util.List;



@Transactional(timeout = 36000, rollbackFor = Exception.class)
@Slf4j
@Service
public class UserImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;



    @Override
    @Transactional
    public int insert(@RequestBody User record) {
        if (record.getId() != null)
            return userMapper.updateByPrimaryKey(record);
        return userMapper.insert(record);
    }


    @Override
    public User getUser(@PathVariable("id") Integer id){
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public List<User> queryUserForList(@RequestBody User record){
        List<User> list = userMapper.queryUserForList(record);
        return list;
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    /**
     * 调用分页插件完成分页
     * @param
     * @return
     */
    private PageInfo<User> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> sysMenus = userMapper.selectPage();
        return new PageInfo<User>(sysMenus);
    }


    @Value("${server.port}")
    String port;
    @Override
    public String getConfig(@RequestParam String num) {
        try{

            return "hello world from port "+port+"------value:"+num;
        }
        catch (Exception ce){
            log.error(ce.getMessage());
        }
        return "";
    }


    @Override
    public String setCache(String val){

        RedisUtil.set("rediskey",val);
        String rediskey = redisUtil.get("rediskey").toString();
        System.out.println(rediskey);

        return rediskey;
    }

}
